#include "video.h"
#include <QMouseEvent>

//**************************************************************
//
//                    CAM SCANNER - PROJECT
//
// version 1.0 - 25.06.2012 Adriano:
//    : first version
//
//**************************************************************


#define SCENEWIDTH      1280
#define SCENEHEIGHT     960

//*****************************************************************************************************************
video::video(QWidget *parent) :
    QGraphicsVideoItem()
{
}

//*****************************************************************************************************************
// overwrite paint function
void video::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
    QGraphicsVideoItem::paint(painter, option, widget);
}


//*****************************************************************************************************************
// Constructor
videoView::videoView(QWidget *parent) :
    QGraphicsView(parent)
{
    this->setGeometry(-SCENEWIDTH/3.35, -SCENEHEIGHT/2.28, SCENEWIDTH, SCENEHEIGHT);
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    m_scene = new QGraphicsScene(this);
    m_scene->setSceneRect(0,0,SCENEWIDTH,SCENEHEIGHT);

    m_videoWidget = new video(this);
    m_videoWidget->setPos(0,0);
    m_videoWidget->setSize(QSize(SCENEWIDTH, SCENEHEIGHT));

    m_scene->addItem(m_videoWidget);

        // add a rectangle, in the future it should be possible to crop the image to this rect.
    m_rect = m_scene->addRect(parentWidget()->x() + 35, parentWidget()->y() + 62, parentWidget()->width() - 38, parentWidget()->height() - 6, QPen(Qt::yellow));

    this->setScene(m_scene);
}

//*****************************************************************************************************************
// Get video widget - widget for camera
QGraphicsVideoItem* videoView::getVideoWidget(void)
{
    return m_videoWidget;
}

//*****************************************************************************************************************
// Get mouse events - for resizing crop rectangle
void videoView::mouseMoveEvent(QMouseEvent *event)
{
    int x = event->pos().x() /*- this->x()*/ + parentWidget()->x();
    int y = event->pos().y() /*+ this->y()*/ + parentWidget()->y();
    int x1 = m_rect->rect().x();
    int x2 = m_rect->rect().width() + x1;
    int y1 = m_rect->rect().y();
    int y2 = m_rect->rect().height() + y1;

    if(x < parentWidget()->width() / 2)
    {
        x1 = x;
    }
    else
    {
        x2 = x;
    }

    if(y - 297 < parentWidget()->height() / 2)
    {
        y1 = y - 120;
    }
    else
    {
        y2 = y - 120;
    }

    m_rect->setRect(x1, y1, x2-x1, y2-y1);

}



